package com.compty.paint.web.member;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.compty.paint.freemarker.CiyiGenerate;
import com.compty.paint.freemarker.DdhjGenerate;
import com.compty.paint.freemarker.IndexGenerate;
import com.compty.paint.freemarker.MarketIndexGenerate;
import com.compty.paint.freemarker.OtherResourceGenerate;
import com.compty.paint.freemarker.PainterStarGenerate;
import com.opensymphony.xwork2.ActionSupport;

import freemarker.template.TemplateException;

public class GenerateHtmlAction  extends ActionSupport  implements ServletRequestAware, ServletResponseAware{

	private static final long serialVersionUID = 2133287687470663086L;
	private Logger logger = Logger.getLogger(GenerateHtmlAction.class);
	
    protected transient HttpServletRequest request;
    protected transient HttpServletResponse response;
	protected Date startDate;
	protected Date endDate;
    
    public String prepare()  {
    	return SUCCESS;
    }

    /**
     * 整站发布
     */
	public String all()  {
		
		String returnMsg = "整个站点发布成功。";
		try {
			IndexGenerate indexGenerate = new IndexGenerate();//生成主页
			indexGenerate.generateHtml();
			
			PainterStarGenerate painterStarGenerate = new PainterStarGenerate(startDate);//中国名家
			painterStarGenerate.generateHtml();
			
			MarketIndexGenerate marketIndexGenerate = new MarketIndexGenerate();//市场(名家)行情
			marketIndexGenerate.generateHtml();
			
			OtherResourceGenerate otherResourceGenerate = new OtherResourceGenerate();
			otherResourceGenerate.generateHtml();
		
		} catch (TemplateException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"模板解析错误。";
		} catch (IOException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"读写文件错误。";
		}catch (Exception e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"整个站点发布失败。";
		}
		
		request.setAttribute("returnMsg", returnMsg);
		logger.info(returnMsg);
		
		return SUCCESS;
	}
	
	/**
	 * 发布首页
	 * @return
	 */
	public String index()  {
		
		String returnMsg = "首页发布成功。";
		try {
			IndexGenerate indexGenerate = new IndexGenerate();//生成主页
			indexGenerate.generateHtml();
			
			OtherResourceGenerate otherResourceGenerate = new OtherResourceGenerate();
			otherResourceGenerate.generateHtml();
		
		} catch (TemplateException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"模板解析错误。";
		} catch (IOException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"读写文件错误。";
		}catch (Exception e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"首页发布失败。";
		}
		
		request.setAttribute("returnMsg", returnMsg);
		logger.info(returnMsg);
		
		return SUCCESS;
	}
	
	/***
	 * 名画名家
	 */
	public String painterStar()  {
		
		String returnMsg = "名画名家发布成功。";
		try {
			PainterStarGenerate painterStarGenerate = new PainterStarGenerate(startDate);
			painterStarGenerate.generateHtml();
			
			OtherResourceGenerate otherResourceGenerate = new OtherResourceGenerate();
			otherResourceGenerate.generateHtml();
		
		} catch (TemplateException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"模板解析错误。";
		} catch (IOException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"读写文件错误。";
		}catch (Exception e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"名画名家发布失败。";
		}
		
		request.setAttribute("returnMsg", returnMsg);
		logger.info(returnMsg);
		
		return SUCCESS;
	}
	
	/**
	 * 市场行情
	 */
	public String marketIndex()  {
		
		String returnMsg = "书画行情发布成功。";
		try {
			MarketIndexGenerate marketIndexGenerate = new MarketIndexGenerate();//市场(名家)行情
			marketIndexGenerate.generateHtml();
			
			OtherResourceGenerate otherResourceGenerate = new OtherResourceGenerate();
			otherResourceGenerate.generateHtml();
		
		} catch (TemplateException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"模板解析错误。";
		} catch (IOException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"读写文件错误。";
		}catch (Exception e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"书画行情发布失败。";
		}
		
		request.setAttribute("returnMsg", returnMsg);
		logger.info(returnMsg);
		
		return SUCCESS;
	}
	
	/**
	 * 瓷艺频道
	 */
	public String ciyi()  {
		
		String returnMsg = "瓷艺频道发布成功。";
		try {
			CiyiGenerate ciyiGenerate = new CiyiGenerate(startDate);
			ciyiGenerate.generateHtml();
			
			OtherResourceGenerate otherResourceGenerate = new OtherResourceGenerate();
			otherResourceGenerate.generateHtml();
		
		} catch (TemplateException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"模板解析错误。";
		} catch (IOException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"读写文件错误。";
		}catch (Exception e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"瓷艺频道发布失败。";
		}
		
		request.setAttribute("returnMsg", returnMsg);
		logger.info(returnMsg);
		
		return SUCCESS;
	}
	
	/**
	 * 当代画家频道
	 */
	public String ddhj()  {
		
		String returnMsg = "当代画家频道发布成功。";
		try {
			DdhjGenerate ddhjGenerate = new DdhjGenerate(startDate);
			ddhjGenerate.generateHtml();
			
			OtherResourceGenerate otherResourceGenerate = new OtherResourceGenerate();
			otherResourceGenerate.generateHtml();
		
		} catch (TemplateException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"模板解析错误。";
		} catch (IOException e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"读写文件错误。";
		}catch (Exception e) {
			logger.error(e.getMessage());
			returnMsg = e.getMessage()+"当代画家频道发布失败。";
		}
		
		request.setAttribute("returnMsg", returnMsg);
		logger.info(returnMsg);
		
		return SUCCESS;
	}
	
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
	}
	
}
